/*
 * Copyright (c) 2023, LXMS (lxinet.cn).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.lxinet.lxms.framework.core.vo;

import cn.lxinet.lxms.framework.core.message.CommonErrorCode;
import cn.lxinet.lxms.framework.core.message.ErrorCode;
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.servlet.ServletResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.PrintWriter;
import java.io.Serializable;

/**
 * @author zcx
 * @date 2023/07/01
 */
@Slf4j
@Schema(description = "接口返回对象")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable{
    @Schema(description = "错误码，0 表示操作成功")
    private int code;
    @Schema(description = "提示信息")
    private String msg;
    @Schema(description = "数据")
    private T data;
    @Schema(description = "分页信息")
    private Page page;

    public Result() {

    }

    public Result(ErrorCode errorCode) {
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
    }


    public Result(T data) {
        if (data instanceof Boolean){
            Boolean flag = (Boolean) data;
            if (flag == true){
                this.code = CommonErrorCode.SUCCESS.getCode();
                this.msg = CommonErrorCode.SUCCESS.getMsg();
            }else {
                this.code = CommonErrorCode.FAIL.getCode();
                this.msg = CommonErrorCode.FAIL.getMsg();
            }
        }else {
            this.code = CommonErrorCode.SUCCESS.getCode();
            this.msg = CommonErrorCode.SUCCESS.getMsg();
            this.data = data;
        }
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static Result success(){
        Result result = new Result();
        result.setCode(CommonErrorCode.SUCCESS.getCode());
        result.setMsg(CommonErrorCode.SUCCESS.getMsg());
        return result;
    }

    public static Result success(Object data){
        Result result = new Result(data);
        result.setCode(CommonErrorCode.SUCCESS.getCode());
        result.setMsg(CommonErrorCode.SUCCESS.getMsg());
        return result;
    }

    public static Result success(Object data, Page page){
        Result result = new Result(data);
        result.setPage(page);
        return result;
    }

    public static Result response(int code, String msg, Object data) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static void responseJson(ServletResponse response, Object data) {
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            out = response.getWriter();
            out.println(JSON.toJSONString(data));
            out.flush();
        } catch (Exception e) {
            log.error("response输出json异常", e);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    public static void responseJson(ServletResponse response, ErrorCode errorCode) {
        Result result = new Result(errorCode);
        responseJson(response, result);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    @Override
    public String toString() {
        return super.toString();
    }
}
